package cn.e4j.modular.crm.controller.base;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.common.exception.BizExceptionEnum;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GwacT;
import cn.e4j.modular.crm.entity.GwaclT;
import cn.e4j.modular.crm.service.impl.Gwai210ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 交易币别控制类
 *
 * @author yuan_rz
 * @Date 2019/01/11 11:02
 */
@Controller
@RequestMapping("/base/gwai210")
public class Gwai210Controller extends BaseController {

    @Autowired
    private Gwai210ServiceImpl gwai210Service;

    /**
     * 跳转页面
     */
    @RequiresPermissions("base:gwai210:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/base/gwai210.html";
    }

    /**
     * 查询列表数据
     */
    @RequestMapping("/getListData")
    @RequiresPermissions("base:gwai210:init")
    @ResponseBody
    public ResponseData getListData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwai210Service.getListData(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 导出Excel
     */
    @RequestMapping(value="/exportListData",method=RequestMethod.GET)
    @RequiresPermissions("base:gwai210:init")
    public void exportListData(ModelMap map, HttpServletRequest request, HttpServletResponse response){

    }

    /**
     * 添加数据
     */
    @RequestMapping("/addData")
    @RequiresPermissions("base:gwai210:init")
    @ResponseBody
    public ResponseData addData(GwacT gwacT, GwaclT gwaclT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwacT.setGWACENT(shiroUser.getDBC());
        gwaclT.setGWACLENT(shiroUser.getDBC());
        gwaclT.setGWACL002(LangUtils.getLang(shiroUser.getLANG()));
        if(gwai210Service.existedTheParams(gwacT)){
            return new ErrorResponseData(BizExceptionEnum.EXISTED_THE_PARAMS.getCode() ,
                    BizExceptionEnum.EXISTED_THE_PARAMS.getMessage());
        }
        gwai210Service.addData(gwacT,gwaclT);
        return SUCCESS_TIP;
    }

    /**
     * 修改数据
     */
    @RequestMapping("/editData")
    @RequiresPermissions("base:gwai210:init")
    @ResponseBody
    public ResponseData editData(GwacT gwacT, GwaclT gwaclT,@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        GwacT oldGwacT = new GwacT();
        oldGwacT.setGWACENT(gwacT.getGWACENT());
        oldGwacT.setGWAC001(inDto.getString("oldGWAC001"));
        if(!gwacT.getGWAC001().equals(oldGwacT.getGWAC001())){
            if(gwai210Service.existedTheParams(gwacT)){
                return new ErrorResponseData(BizExceptionEnum.EXISTED_THE_PARAMS.getCode() ,
                        BizExceptionEnum.EXISTED_THE_PARAMS.getMessage());
            }
        }
        gwai210Service.editData(gwacT,gwaclT,oldGwacT);
        return SUCCESS_TIP;
    }

    /**
     * 删除数据
     */
    @RequestMapping("/deleteData")
    @RequiresPermissions("base:gwai210:init")
    @ResponseBody
    public ResponseData deleteData(GwacT gwacT, GwaclT gwaclT) {
        gwai210Service.deleteData(gwacT,gwaclT);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/deleteDataBatch")
    @RequiresPermissions("base:gwai210:init")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwacT> gwacTList = inDto.getReqList(GwacT.class);
        List<GwaclT> gwaclTList = inDto.getReqList(GwaclT.class);
        gwai210Service.deleteDataBatch(gwacTList,gwaclTList);
        return SUCCESS_TIP;
    }

}
